﻿
using DbProject;
using MvcEasyProject.Models;
using MvcEasyProject.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebMatrix.WebData;

namespace MvcEasyProject.Controllers
{
    [Authorize]
    public class UsersController : Controller
    {

        public string UserName
        {
            get
            {
                using (DbProjectContext contextDB = new DbProjectContext())
                {
                    var Worker = contextDB.Workers.FirstOrDefault(u => u.WorkerId == WebSecurity.CurrentUserId);
                    return Worker.FirstName + " " + Worker.LastName;
                }

            }
        }
        //
        // GET: /Users/

        private DbProjectContext db = new DbProjectContext();
        public ActionResult Index()
        {
            UsersPageModel Users = new UsersPageModel();
            if (User.IsInRole("Administrator"))
            {
              var x=  User.Identity.AuthenticationType;
                //return View(db.Users.ToList());
               Users.Users = db.Users.ToList() ;
               
             //RoleProvider.GetUsersInRole("Manager");
     
                
                return View(Users);
            }

            if (User.IsInRole("Manager"))
            {
                foreach (var userName in Roles.GetUsersInRole("Worker"))
                {
                    Users.Users.Add(db.Users.FirstOrDefault(dbU => dbU.UserName == userName));
                }
                return View(User.IsInRole("Worker"));
            }
         
            return View(db.Users.ToList());
        }



        //
        // GET: /Users/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Users/Create
        [Authorize(Roles="Administrator")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Users/Create

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Users/Edit/5

        public ActionResult Edit(int id=0)
        {
            return View();
        }

        //
        // POST: /Users/Edit/5

        [HttpPost]
        public ActionResult Edit(UserModel userModel)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Users/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Users/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
